package com.ucas.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ucas.dao.UserRespositroy;
import com.ucas.model.SelfCheckEntity;
import com.ucas.model.User;
import com.ucas.redis.RedisUtil;

import redis.clients.jedis.Jedis;

@Controller
public class TestController {
	@Autowired
	private UserRespositroy userRespositroy;
	
	@RequestMapping("/")
	public String toLoginPage(HttpServletRequest request,HttpServletResponse response) {
		Jedis jedis = RedisUtil.getJedis();
//		System.out.println("redis: name = "+jedis.get("name"));
//		RedisUtil.returnResource(jedis);
//		response.setHeader("name","zhangsan");
		return "login";
	}
	@RequestMapping("/login.do")
	@ResponseBody
	public Map<String,Object> login(HttpServletRequest request,HttpServletResponse response,String userCode,String password) {
		Map<String,Object> result = new HashMap<String,Object>();
		HttpSession session = request.getSession();
		if("admin".equals(userCode) && "admin".equals(password)) {
			result.put("success",true);
			session.setAttribute("login",true);
			session.setAttribute("user",userCode);
		}else {
			result.put("success",false);
		}
		return result;
	}
	@RequestMapping("/lxi-list.do")
	public String toTestPage(HttpServletRequest request,HttpServletResponse response,Model model) {
		HttpSession session = request.getSession();
		if(session.getAttribute("login") == null) {
			return "login";
		}else {
			model.addAttribute("name",request.getSession().getAttribute("user"));
			return "lxi-list";
		}
	}
	@RequestMapping("/lxi-item.do")
	public String toIndex(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		if(session.getAttribute("login") == null) {
			return "login";
		}else {
			Jedis jedis = RedisUtil.getJedis();
			RedisUtil.returnResource(jedis);
			return "lxi-detail";
		}
	}
	@RequestMapping("/aa.do")
	public void test111(HttpServletRequest quest,HttpServletResponse response) {
		PrintWriter out = null;
		try {
			List<User> list = userRespositroy.findAll();
			out = response.getWriter();
			out.write(list.get(0).getName());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/test.do")
	@ResponseBody
	public SelfCheckEntity test(HttpServletRequest quest,HttpServletResponse response) {
		SelfCheckEntity tm = new SelfCheckEntity();
		tm.sc_10ge.Nums=5;
		tm.sc_10ge.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_ad.Nums=5;
		tm.sc_ad.Status=new float[]{1f,2f,3f,4f,5f};
		
		tm.sc_can.Nums=5;
		tm.sc_can.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_da.Nums=5;
		tm.sc_da.Status=new float[]{1f,2f,3f,4f,5f};
		
		tm.sc_ge.Nums=5;
		tm.sc_ge.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_lvds.Nums=5;
		tm.sc_lvds.Status=new boolean[]{true,false,true,true,true};
		
//		tm.sc_mt.Nums=5;
//		tm.sc_mt.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_rt.Nums=5;
		tm.sc_rt.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_oc.Nums=5;
		tm.sc_oc.Status=new float[]{1f,2f,3f,4f,5f};
		
		tm.sc_rs232.Nums=5;
		tm.sc_rs232.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_rs422.Nums=5;
		tm.sc_rs422.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_rs485.Nums=5;
		tm.sc_rs485.Status=new boolean[]{true,false,true,true,true};
		
		tm.sc_lxi.instrument = "instrument";
		tm.sc_lxi.serizlNum = "serizlNum";
		tm.sc_lxi.description = "description";
		tm.sc_lxi.ip = "127.0.0.1";
		tm.sc_lxi.mac = "10.0.255.255";
		
		return tm;
	}
}
